
#pragma comment(lib,"glew32.lib")

#include <stdio.h>
#include <stdlib.h>
#include "glew.h"

#include <gl/glut.h>

#include "CReader.h"

GLuint v, f, p;
float lpos[4] = { 1, 0.5, 1, 0 };

GLint time_id;

void changeSize(int w, int h)
{
	if (h == 0) h = 1;
	float ratio = 1.0 *w / h;

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	glViewport(0, 0, w, h);

	gluPerspective(45,  ratio, 1,  1000);
	glMatrixMode(GL_MODELVIEW);


}

float a = 0;


void renderScene(void)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();
	gluLookAt(0.0, 0.0, 5.0,
		0.0, 0.0, -1.0,
		0.0f, 1.0f, 0.0f);

	glLightfv(GL_LIGHT0, GL_POSITION, lpos);
	glRotatef(a, 0, 0, 1);
	glutWireTeapot(1);
	a += 0.1;
	glUniform1f(time_id, a);
	glutSwapBuffers();

}

void setShaders()
{
	GLint v, f;
	v = glCreateShader(GL_VERTEX_SHADER);
	f = glCreateShader(GL_FRAGMENT_SHADER);

	char*vs = NULL, *fs = NULL;
	CReader reader;

	vs = reader.textFileRead("passthrough.vert");
	fs = reader.textFileRead("uniform.frag");
	printf("%s\n", vs);
	printf("%s\n", fs);
	const char * vv = vs;
	const char * ff = fs;
	glShaderSource(v, 1, &vv, NULL);
	glShaderSource(f, 1, &ff, NULL);

	free(vs);
	free(fs);
	glCompileShader(v);
	glCompileShader(f);

	p = glCreateProgram();
	glAttachShader(p, v);
	glAttachShader(p, f);

	glLinkProgram(p);
	glUseProgram(p);



	time_id = glGetUniformLocation(p, "v_time");

	
	
}


int main(int argc, char **argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);

	


	glutInitWindowPosition(100, 100);
	glutInitWindowSize(800, 800);
	glutCreateWindow("GPGPU Tutorial");

	glutDisplayFunc(renderScene);
	glutIdleFunc(renderScene);
	glutReshapeFunc(changeSize);

	glEnable(GL_DEPTH_TEST);

	glClearColor(0.0, 0.0, 0.0, 1.0);
	glColor3f(1.0, 1.0, 1.0);
	glEnable(GL_CULL_FACE);
	glewInit();

	setShaders();

	glutMainLoop();

	return 0;
}